﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
using System.Configuration;
using System.Reflection;
using log4net;

namespace Xtee.Core.Adapter.Config
{
    public abstract class BaseConfigurator
    {
        
        private static readonly object syncRoot = new Object(); 
        protected static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 
       


        protected IAdapterConfiguration LoadAdapterConfiguration(string configPath)
        {
            lock (syncRoot)
            {
                try
                {
                    object configsection;
                    if (string.IsNullOrEmpty(configPath))
                    {
                        configsection = System.Configuration.ConfigurationManager.GetSection(GetSectionName());
                        if (configsection == null)
                            throw new ConfigurationErrorsException("Unable to find configuration section -> " + GetSectionName() + " from  configuration file!");
                    }

                    else
                    {
                        var fmap = new ExeConfigurationFileMap();
                        fmap.ExeConfigFilename = configPath;
                        Configuration mappedconfig = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fmap,
                                                                                                             ConfigurationUserLevel
                                                                                                                 .None);
                        configsection = mappedconfig.GetSection(GetSectionName());
                        if (configsection == null)
                            throw new ConfigurationErrorsException("Unable to find configuration section -> " + GetSectionName() + " from  configuration file (" + configPath + ")!");
                    }
                   
                    return (IAdapterConfiguration)configsection;
                }
                catch (Exception ex)
                {
                    Log.Error("Exception occured while loading configuration, check Web.config.", ex);
                    throw;
                }
            }
            
          


        }
        protected abstract string GetSectionName(); 
    }
}
